package com.example.modlemain.ui.custom

import android.content.Context
import android.graphics.Canvas
import android.graphics.drawable.Drawable
import android.util.AttributeSet
import androidx.appcompat.widget.AppCompatTextView
import com.example.modlemain.R


class MultiIconTextView(context: Context, attrs: AttributeSet?) :
    AppCompatTextView(context, attrs) {
    private var iconPadding = 0f
    private var firstLineOffset = 0f
    private val icons: ArrayList<Drawable> = ArrayList()

    init {
        attrs?.let {
            val typedArray = context.obtainStyledAttributes(it, R.styleable.MultiIconTextView)
            val oneIconResource = typedArray.getResourceId(
                R.styleable.MultiIconTextView_oneIcon,
               -1
            )
            val twoIconResource = typedArray.getResourceId(
                R.styleable.MultiIconTextView_twoIcon,
                -1
            )
            iconPadding = typedArray.getDimension(
                R.styleable.MultiIconTextView_iconPadding,
                0f
            )
            if(oneIconResource!=-1) {
                context.getDrawable(oneIconResource)?.let { it1 -> icons.add(it1) }
            }
            if(twoIconResource!=-1) {
                context.getDrawable(twoIconResource)?.let { it1 -> icons.add(it1) }
            }
            typedArray.recycle()
        }
    }

    override fun onDraw(canvas: Canvas) {

        // 保存画布状态
        canvas.save()


        // 绘制第一行图标
        if (!icons.isEmpty() && layout != null) {
            val layout = layout
            val lineTop = layout.getLineTop(0)
            val baseline = layout.getLineBaseline(0)
            var x = paddingLeft


            // 计算第一行偏移量
            firstLineOffset = 0f
            for (icon in icons) {
                val iconHeight = baseline - lineTop
                val iconWidth = (icon.intrinsicWidth *
                        (iconHeight.toFloat() / icon.intrinsicHeight)).toInt()

                icon.setBounds(0, 0, iconWidth, iconHeight)
                canvas.save()
                canvas.translate(x.toFloat(), lineTop.toFloat())
                icon.draw(canvas)
                canvas.restore()

                x = (x + (iconWidth + iconPadding)).toInt()
                firstLineOffset += iconWidth + iconPadding
            }


            // 移动第一行文本绘制位置
            canvas.translate(firstLineOffset, 0f)
        }


        // 绘制文本（自动处理第二行对齐）
        super.onDraw(canvas)


        // 恢复画布状态
        canvas.restore()


        // 特殊处理：第二行及以后的行需要取消偏移
        if (firstLineOffset > 0 && layout != null) {
            canvas.save()
            for (i in 1 until layout.lineCount) {
                val lineStart = layout.getLineStart(i)
                val lineEnd = layout.getLineEnd(i)
                val lineLeft = layout.getLineLeft(i)


                // 绘制时取消第一行的偏移量
                canvas.drawText(
                    text.toString(),
                    lineStart,
                    lineEnd,
                    lineLeft,
                    layout.getLineBaseline(i).toFloat(),
                    paint
                )
            }
            canvas.restore()
        }
    }

}